package com.xinyiweidai.xinyiweidai.module.bind;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.LoginBean;
import com.xinyiweidai.xinyiweidai.event.BindingEvent;
import com.xinyiweidai.xinyiweidai.util.Api;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;

/**
* 项目名：com.xinyiweidai.xinyiweidai
* 类名： BindingActivity
* 创建人：Lan
* 创建时间：2018/9/8 22:02
* 描述：  绑定认证
*/
public class BindingActivity extends BaseActivity {


    private TextView tv_bing_identity,tv_bing_card,tv_bing_contacts,tv_bing_operator,tv_bing_bank;
    private LinearLayout ll_bing_identity,ll_bing_card,ll_bing_contacts,ll_bing_operator,ll_bing_bank;
    private String sfz_status,yhk_status,yys_status,txl_status,htyz_status;



    @Override
    protected void receiveData() {

        EventBus.getDefault().register(this);

        sfz_status= SpUtil.getInstance().getUserInfo().getSfz();
        htyz_status= SpUtil.getInstance().getUserInfo().getHtyz();
        txl_status= SpUtil.getInstance().getUserInfo().getTxl();
        yys_status= SpUtil.getInstance().getUserInfo().getYys();
        yhk_status= SpUtil.getInstance().getUserInfo().getYhk();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_binding;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back,"绑定认证",0,true);

    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_bing_identity = (TextView) findViewById(R.id.tv_bing_identity);
        tv_bing_card = (TextView) findViewById(R.id.tv_bing_card);
        tv_bing_contacts = (TextView) findViewById(R.id.tv_bing_contacts);
        tv_bing_operator = (TextView) findViewById(R.id.tv_bing_operator);
        tv_bing_bank = (TextView) findViewById(R.id.tv_bing_bank);

        ll_bing_identity = (LinearLayout) findViewById(R.id.ll_bing_identity);
        ll_bing_card = (LinearLayout) findViewById(R.id.ll_bing_card);
        ll_bing_contacts = (LinearLayout) findViewById(R.id.ll_bing_contacts);
        ll_bing_operator = (LinearLayout) findViewById(R.id.ll_bing_operator);
        ll_bing_bank = (LinearLayout) findViewById(R.id.ll_bing_bank);

        changeStatus();
    }


    private void changeStatus() {
        changeViewStatus(sfz_status,tv_bing_identity);
        changeViewStatus(htyz_status,tv_bing_card);
        changeViewStatus(txl_status,tv_bing_contacts);
        changeViewStatus(yys_status,tv_bing_operator);
        changeViewStatus(yhk_status,tv_bing_bank);
    }


    private void changeViewStatus(String name,TextView tv){
        if (name.equals("1")){
            tv.setText("已认证");
            tv.setTextColor(getResources().getColor(R.color.appColor));
            tv.setBackground(getResources().getDrawable(R.drawable.tv_green));
        }
    }


    @Override
    protected void addClickEvent() {
        ll_bing_identity.setOnClickListener(view -> judgeBindStatus("1",sfz_status,IdentityActivity.class));
        ll_bing_card.setOnClickListener(view -> judgeBindStatus(sfz_status,htyz_status,CardActivity.class));
        ll_bing_contacts.setOnClickListener(view -> BindingActivity.this.judgeBindStatus(htyz_status, txl_status, ContactsActivity.class));
        ll_bing_operator.setOnClickListener(view -> judgeBindStatus(txl_status,yys_status,OperatorActivity.class));
        ll_bing_bank.setOnClickListener(view -> judgeBindStatus(yys_status,yhk_status,BankActivity.class));
    }


    private void judgeBindStatus(String aboveName,String nowName ,Class<?> clazz){
        startNewActivity(clazz);
//        if (!aboveName.equals("1")){
//            MyToast.showError(this,"请完成上一步");
//        }else if(nowName.equals("1")){
//            MyToast.showSuccess(this,"已认证");
//        }else{
//
//        }
    }


    //  认证完成后刷新展示颜色
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBindingEvent(BindingEvent bindEvent) {
        refreshLogin(bindEvent);
    }


        //刷新状态
        private void refreshLogin(BindingEvent bindEvent) {
            Map<String,String> map=new HashMap<>();
            map.put("token",SpUtil.getInstance().getUserInfo().getToken());
            OkHttpUtils.get().url(Api.refresh).params(map).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                    MyToast.showError(BindingActivity.this,"网络异常");
                }

                @Override
                public void onResponse(String response) {
                    L.e("bind"+response);
                    LoginBean bean=new Gson().fromJson(response,LoginBean.class);
                    if (bean.getCode()==1){
                        SpUtil.getInstance().saveUserInfo(bean.getData().getUserinfo());

                        if (bindEvent.getMessage().equals("status_1")){
                            sfz_status= SpUtil.getInstance().getUserInfo().getSfz();
                            changeViewStatus(sfz_status,tv_bing_identity);
                            return;
                        }
                        if (bindEvent.getMessage().equals("status_2")){
                            htyz_status= SpUtil.getInstance().getUserInfo().getHtyz();
                            changeViewStatus(htyz_status,tv_bing_card);
                            return;
                        }
                        if (bindEvent.getMessage().equals("status_3")){
                            txl_status= SpUtil.getInstance().getUserInfo().getTxl();
                            changeViewStatus(txl_status,tv_bing_contacts);
                            return;
                        }
                        if (bindEvent.getMessage().equals("status_4")){
                            yys_status= SpUtil.getInstance().getUserInfo().getYys();
                            changeViewStatus(yys_status,tv_bing_operator);
                            return;
                        }
                        if (bindEvent.getMessage().equals("status_5")){
                            yhk_status= SpUtil.getInstance().getUserInfo().getYhk();
                            changeViewStatus(yhk_status,tv_bing_bank);
                        }

                    }
                }
            });
        }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
